package com.example.maxboxtwo.components.function.Compass;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.maxboxtwo.R;

public class CompassView extends View {
    private Bitmap compass;
    private Bitmap innerPoint;
    private Paint paint;
    private Path path;
    private Integer degree=0;

    public Integer getDegree() {
        return degree;
    }

    public void setDegree(Integer degree) {
        this.degree = degree;
    }

    public CompassView(Context context) {
        super(context);
    }

    public CompassView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CompassView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(compass,(getWidth()-compass.getWidth())/2,(getHeight()-compass.getHeight())/2,paint);
        canvas.rotate(getDegree(),getWidth()/2,getHeight()/2);
        canvas.drawBitmap(innerPoint,(getWidth()-innerPoint.getWidth())/2,(getHeight()-innerPoint.getHeight())/2,paint);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if(paint==null){
        paint = new Paint();
        // against shake
        paint.setDither(true);
        // against Rectangle
        paint.setAntiAlias(true);
        // set pen's graphic
        paint.setStrokeCap(Paint.Cap.ROUND);
        }

        if(innerPoint==null){
            innerPoint = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.compass_point));
        }

        if(compass==null){
            compass = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.compass_compass_bg),innerPoint.getWidth()+250,innerPoint.getHeight()+250,true);
        }
    }


}
